package com.work.music.utils;

import com.work.music.common.RedisSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

public class PhoneCode {
    private static Logger logger = LoggerFactory.getLogger("runtimeLogger");
    /**
     * 判断获取验证码是否过于频繁
     * @param phone
     * @param type
     * @return
     */
    public static boolean isFrequently(RedisSession redisSession, String phone, String type) {
        Map<String, Object> phoneCode = redisSession.getMapValue("yzcode" + phone);
        if (StringUtil.isMapNull(phoneCode))
            return false;
        String time = phoneCode.get("time") + "";
        String codeType = phoneCode.get("type") + "";
        if (StringUtil.isNull(time) || StringUtil.isNull(type))
            return false;
        if (!codeType.equals(type))
            return false;
        long nowtime = System.currentTimeMillis();
        if ((nowtime - Long.parseLong(time)) / 1000 / 60 >= 1)
            return false;
        return true;
    }

    /**
     * 添加验证码
     * @param redisSession
     * @param phone
     * @param type
     * @param yzcode
     */
    public static void addPhoneCode(RedisSession redisSession, String phone, int type, String yzcode) {
        Map<String, Object> phonecode = new HashMap<String, Object>();
        phonecode.put("time", new Date().getTime());
        phonecode.put("type", type);
        phonecode.put("code", yzcode);
        redisSession.setMapValue("yzcode" + phone, phonecode, 5, TimeUnit.MINUTES);
    }

    /**
     * 判断验证码是否正确
     * @param redisSession
     * @param phone
     * @param type
     * @param code
     * @return
     */
    public static int yzcode(RedisSession redisSession, String phone, String type, String code) {
        Map<String, Object> phonecode = redisSession.getMapValue("yzcode" + phone);
        if(phonecode == null || phonecode.size() <= 0)
            return -1;   //验证码已过期
        if(!code.equals(phonecode.get("code") + "") || !type.equals(phonecode.get("type") + ""))
            return -2;  //验证码错误
        return 0;
    }

    /**
     * 判断验证验证码后是否超时及是否错误
     * @param redisSession
     * @param phone
     * @param yzcode
     * @param type
     * @return
     */
    public static int yzcodeTimeOut(RedisSession redisSession, String phone, String yzcode, int type) {
        Map<String, Object> phoneCode = redisSession.getMapValue("yzcode" + phone);
        if(StringUtil.isMapNull(phoneCode))
            return -1;  //验证超时
        String code = phoneCode.get("code") + "";
        int yztype;
        long time;
        try {
            time = Long.parseLong(phoneCode.get("time") + "");
            yztype = Integer.parseInt(phoneCode.get("type") + "");
        } catch (NumberFormatException e) {
            return -1;
        }
        long nowTime = System.currentTimeMillis();
        if((nowTime - time) > 30 * 60 * 1000)
            return -1;
        if(!yzcode.equals(code) || type != yztype)
            return -2;
        return 0;
    }

    /**
     * 发送验证码
     * @param phone
     * @param phonecode
     * @return
     */
    public static Map<String, Object> sendPhoneCode(String phone, String phonecode) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "0");
        map.put("reason", "发送成功");

        return map;
    }

    /**
     * 删除缓存中的数据
     * @param phone
     */
    public static void removeYzcode(RedisSession redisSession, String phone) {
        redisSession.unlock("yzcode" + phone);
    }

    /**
     * 删除缓存中的用户信息
     * @param redisSession
     * @param username
     * @param id
     * @return
     */
    public static List<String> removeUser(RedisSession redisSession, String username, Long id) {
        List<String> list = redisSession.getListValue("userList");
        List<String> resultList = new ArrayList<>();
        if(!StringUtil.isListNull(list)) {
            for (String str: list) {
                Map<String, Object> map = redisSession.getMapValue(str);
                if(!StringUtil.isMapNull(map)) {
                    if(id.equals(Long.parseLong(map.get("uid") + ""))) {
                        redisSession.unlock(str);
                    } else {
                        resultList.add(str);
                    }
                }
            }
        }
        return resultList;
    }


    public static void main(String[] args) {
        Long ss = 2L;
        String str = "2";
        System.out.println(ss.equals(Long.parseLong(str)));
    }
}
